package com.xuecheng.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CoursePublishPreMapper;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.model.po.CoursePublishPre;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePublishPreService;
import com.xuecheng.content.service.CourseTeacherService;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
public class CoursePublishPreServiceImpl extends ServiceImpl<CoursePublishPreMapper, CoursePublishPre> implements CoursePublishPreService {
    @Autowired
    CourseBaseService courseBaseService;

    @Autowired
    TeachplanService teachplanService;

    @Autowired
    CourseTeacherService courseTeacherService;

    @Autowired
    CoursePublishPreMapper coursePublishPreMapper;

    @Autowired
    CourseBaseMapper courseBaseMapper;
    @Override
    @Transactional
    public void saveCourse(Long courseId) {
        //TODO:当前为等待审核状态，审核完成可以再次提交。不能重复提交
//        //约束校验
//        CourseBase courseBase = courseBaseMapper.selectById(courseId);
//        //课程审核状态
//        String auditStatus = courseBase.getAuditStatus();
//        //当前审核状态为已提交不允许再次提交
//        if("202003".equals(auditStatus)){
//            XueChengPlusException.cast("当前为等待审核状态，审核完成可以再次提交。");
//        }
        //课程基本信息和营销信息
        CourseBaseInfoDto courseBaseInfo = courseBaseService.getCourseBaseInfo(courseId);
        CoursePublishPre coursePublishPre = new CoursePublishPre();
        //插入基本信息
        BeanUtils.copyProperties(courseBaseInfo,coursePublishPre);
        //插入营销信息
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(courseBaseInfo,courseMarket);
        String courseMarketJson = JSON.toJSONString(courseMarket);
        coursePublishPre.setMarket(courseMarketJson);
        //插入课程计划信息
        //课程计划信息
        List<TeachplanDto> treeNodes = teachplanService.getTreeNodes(courseId);
        String treeNodesJson = JSON.toJSONString(treeNodes);
        coursePublishPre.setTeachplan(treeNodesJson);
        //插入教师信息
        List<CourseTeacher> courseTeachers = courseTeacherService.listTeacher(courseId);
        String courseTeachersJson = JSON.toJSONString(courseTeachers);
        coursePublishPre.setTeachers(courseTeachersJson);
        coursePublishPre.setAuditDate(LocalDateTime.now());
        coursePublishPre.setCreateDate(LocalDateTime.now());
        //设置预发布记录状态,已提交
        coursePublishPre.setStatus("202003");
        //插入数据库
        CoursePublishPre coursePublishPreUpdate = coursePublishPreMapper.selectById(courseId);
        //创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            if (coursePublishPreUpdate == null) {
                //添加课程预发布记录
                coursePublishPreMapper.insert(coursePublishPre);
            } else {
                coursePublishPreMapper.updateById(coursePublishPre);
            }
        }, executorService);
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            //将课程基本信息的审核状态改为已提交
            CourseBase courseBase = new CourseBase();
            BeanUtils.copyProperties(courseBaseInfo, courseBase);
            courseBase.setAuditStatus("202003");
            courseBaseService.updateById(courseBase);
        }, executorService);
        CompletableFuture.allOf(future1,future2);
    }
}
